﻿namespace ChinaUnicom.Themes
{
    using System.Windows.Media;
    using System.Windows;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;

    public enum Theme
    {
        light,
        dark
    }

    public static class ThemesHelper
    {
        #region colors
        static Color? phoneForegroundColor;

        static Theme theme = Theme.light;

        public static Color PhoneForegroundColor
        {
            get
            {
                if (!phoneForegroundColor.HasValue)
                {
                    switch (theme)
                    {
                        case Theme.light:
                            phoneForegroundColor = (Color)Application.Current.Resources["LightPhoneForegroundColor"];
                            break;
                        case Theme.dark:
                            phoneForegroundColor = (Color)Application.Current.Resources["DarkPhoneForegroundColor"];
                            break;
                        default:
                            phoneForegroundColor = (Color)Application.Current.Resources["PhoneForegroundColor"];
                            break;
                    }
                }

                return phoneForegroundColor.Value;
            }
        }

        static Color? phoneBackgroundColor;
        public static Color PhoneBackgroundColor
        {
            get
            {
                if (!phoneBackgroundColor.HasValue)
                {
                    switch (theme)
                    {
                        case Theme.light:
                            phoneBackgroundColor = (Color)Application.Current.Resources["LightPhoneBackgroundColor"];
                            break;
                        case Theme.dark:
                            phoneBackgroundColor = (Color)Application.Current.Resources["DarkPhoneBackgroundColor"];
                            break;
                        default:
                            phoneBackgroundColor = (Color)Application.Current.Resources["PhoneBackgroundColor"];
                            break;
                    }
                }

                return phoneBackgroundColor.Value;
            }
        }

        static Color? applicationBarBackgroundColor;
        public static Color ApplicationBarBackgroundColor
        {
            get
            {
                if (!applicationBarBackgroundColor.HasValue)
                {
                    switch (theme)
                    {
                        case Theme.light:
                            applicationBarBackgroundColor = (Color)Application.Current.Resources["LightPhoneChromeColor"];
                            break;
                        case Theme.dark:
                            applicationBarBackgroundColor = (Color)Application.Current.Resources["DarkPhoneChromeColor"];
                            break;
                        default:
                            applicationBarBackgroundColor = (Color)Application.Current.Resources["PhoneChromeColor"];
                            break;
                    }
                }

                return applicationBarBackgroundColor.Value;
            }
        }

        #endregion

        #region brushes
        static SolidColorBrush phoneForegroundBrush;
        public static SolidColorBrush PhoneForegroundBrush
        {
            get
            {
                if (phoneForegroundBrush == null)
                {
                    phoneForegroundBrush = (SolidColorBrush)Application.Current.Resources["PhoneForegroundBrush"];
                }
                return phoneForegroundBrush;
            }
        }
        static SolidColorBrush phoneBackgroundBrush;
        public static SolidColorBrush PhoneBackgroundBrush
        {
            get
            {
                if (phoneBackgroundBrush == null)
                {
                    phoneBackgroundBrush = (SolidColorBrush)Application.Current.Resources["PhoneBackgroundBrush"];
                }
                return phoneBackgroundBrush;
            }
        }
        static SolidColorBrush phoneSubtleBrush;
        public static SolidColorBrush PhoneSubtleBrush
        {
            get
            {
                if (phoneSubtleBrush == null)
                {
                    phoneSubtleBrush = (SolidColorBrush)Application.Current.Resources["PhoneSubtleBrush"];
                }
                return phoneSubtleBrush;
            }
        }

        #endregion

        public static void SetTheme(PhoneApplicationPage page)
        {
            SetSystemTrayTheme();
            SetApplicationBarTheme(page);
        }

        public static void SetSystemTrayTheme()
        {
            SystemTray.ForegroundColor = PhoneForegroundColor;
            SystemTray.BackgroundColor = PhoneBackgroundColor;
        }

        public static void SetApplicationBarTheme(PhoneApplicationPage page)
        {
            if (page.ApplicationBar == null) return;
            page.ApplicationBar.ForegroundColor = PhoneForegroundColor;
            page.ApplicationBar.BackgroundColor = ApplicationBarBackgroundColor;
        }

    }
}
